﻿using UnityEngine;

public class FPS : MonoBehaviour
{
    private float sampleTime = 0.3f; // 采样时间
    private int frame = 0;              // 经过帧数
    private float time = 0;         // 运行时间
    private float fps = 0;         // 运行时间

    private void Update()
    {
        frame += 1;
        time += Time.deltaTime;

        // 刷新帧率
        if (time >= sampleTime)
        {
            fps = frame / time;
            
            frame = 0;
            time = 0;
        }
    }

    private void OnGUI()
    {
        GUIStyle fontStyle = new GUIStyle();
        fontStyle.normal.textColor = new Color(1, 0, 0);   //设置字体颜色
        fontStyle.fontSize = 40;       //字体大小
        GUILayout.Label("FPS:" + fps.ToString("F2"), fontStyle);
    }
}

